import { Component } from 'react'
import { Provider } from 'react-redux'
import Taro from '@tarojs/taro'

import configStore from './store'

import './app.scss'
// import user from './reducers/user'

const store = configStore()

// 轮询
let timer
function polling(func, time=5000) {
  console.log('刷新')
  timer = setTimeout(() => {
    timer && clearTimeout(timer)

    func()
    polling(func, time=5000)
  }, time);
}

class App extends Component {
  onLaunch() {
    if (!Taro.cloud) {
      console.error('请使用 2.2.3 或以上的基础库以使用云能力');
    } else {
      Taro.cloud.init({
        // env: 'my-env-id',
        traceUser: true,
      });
    }
  }

  getNotify = () => {
    const user = store.getState().user
    Taro.cloud.callFunction({
      name: 'tiezi',
      data: {
        type: 'checkeNotify',
        userId: user.userId
      }
    })
    .then(res => {
      console.log('接收到消息：', res.result)
    })
    .catch(err => {
      console.log(err)
    })
  }

  componentDidMount () {
    polling(this.getNotify)
  }

  componentDidShow () {}

  componentDidHide () {}

  componentDidCatchError () {}

  // 在 App 类中的 render() 函数没有实际作用
  // 请勿修改此函数
  render () {
    return (
      <Provider store={store}>
        {this.props.children}
      </Provider>
    )
  }
}

export default App
